package com.scqkxx.gamebox.activity.myself;

import android.app.Dialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.SaveCallback;
import com.scqkxx.gamebox.R;
import com.scqkxx.gamebox.activity.activities.utils.ActivityUtil;
import com.scqkxx.gamebox.activity.activities.view.TitleView;
import com.scqkxx.gamebox.activity.myself.loginutils.LoginRequestUtils;
import com.scqkxx.gamebox.activity.myself.loginutils.OnRequestListener;
import com.scqkxx.gamebox.activity.myself.spaceUtils.SpaceUtils;
import com.scqkxx.gamebox.activity.photoTag.ImageDotLayout;
import com.scqkxx.gamebox.bean.EvenBusBean;
import com.scqkxx.gamebox.databinding.ActivityAboutUsBinding;
import com.scqkxx.gamebox.utils.AppInfoUtils;
import com.scqkxx.gamebox.utils.DialogUtils;
import com.scqkzqtz.base.base.BaseActivity;
import com.scqkzqtz.base.library.utils.ToastUtils;
import com.scqkzqtz.library.UpdateUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.disposables.Disposable;

public class SettingAboutUsActivity extends BaseActivity<ActivityAboutUsBinding> implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
    private TextView spaceName;
    private String imageUrl;
    private String objectId;
    private int belongGroupId;
    private View inflate;
    private Dialog dialog;
    private EditText goodsName;
    private Switch goodsSwitch;
    private Button tv_Preservation;
    private boolean isPublic = true;
    private String coordinate = "";
    private List<ImageDotLayout.IconBean> iconBeanList;
    int type = 0;
    private TextView tvDelete;
    private ImageDotLayout.IconBean bean;
    private TextView imageView;


    @Override
    public int setLayoutId() {
        return R.layout.activity_about_us;
    }


    @Override
    public void init() {
        initView();
        initIcon();
    }

    private void initData() {
        AVQuery<AVObject> query = AVQuery.getQuery("Goods");
        query.whereEqualTo("belongGroupId", belongGroupId);
        query.whereEqualTo("positionId", objectId);
        query.whereEqualTo("isDisable", 0);
        query.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> list, AVException e) {
                if (e == null) {
                    int index = 1;
                    for (int i = 0; i < list.size(); i++) {
                        AVObject avObject = list.get(i);
                        String coordinate = (String) avObject.get("coordinate");
                        if (!TextUtils.isEmpty(coordinate)) {
                            String[] arr = coordinate.split("\\+");
                            if (arr.length >= 2) {
                                ImageDotLayout.IconBean bean = new ImageDotLayout.IconBean(index, Float.valueOf(arr[0]),
                                        Float.valueOf(arr[1]), (String) avObject.get("name"),
                                        ((Integer) avObject.get("isPublic")) == 1 ? true : false,
                                        avObject.getObjectId());
                                iconBeanList.add(bean);
                                index++;
                            }
                        }
                    }
                    binding.idlIdlPhoto.addIcons(iconBeanList);
                }
            }
        });
    }

    private void initView() {
        imageUrl = getIntent().getStringExtra("imageUrl");
        objectId = getIntent().getStringExtra("objectId");
        String spaceNames = getIntent().getStringExtra("spaceName");
        belongGroupId = getIntent().getIntExtra("belongGroupId", 0);
        iconBeanList = new ArrayList<>();
        //设置背景图片
        binding.idlIdlPhoto.setImage(this.getApplicationContext(), imageUrl);
        binding.idlIdlPhoto.setOnImageClickListener(bean -> {
            //可以一系列处理后再添加标签
            type = 1;
            imageView = binding.idlIdlPhoto.addIcon(bean);
            coordinate = bean.sx + "+" + bean.sy;
            inflate = LayoutInflater.from(SettingAboutUsActivity.this).inflate(R.layout.tag_dialog, null);
            dialog = DialogUtils.showCenterDialog(SettingAboutUsActivity.this, inflate);
            goodsName = (EditText) inflate.findViewById(R.id.goodsNmae);
            spaceName = (TextView) inflate.findViewById(R.id.spaceName);
            spaceName.setText(spaceNames);
            goodsSwitch = (Switch) inflate.findViewById(R.id.goodsSwitch);
            tv_Preservation = (Button) inflate.findViewById(R.id.tv_Preservation);
            goodsSwitch.setOnCheckedChangeListener(SettingAboutUsActivity.this);
            tv_Preservation.setOnClickListener(SettingAboutUsActivity.this);
            dialog.setOnDismissListener(dialog -> {
                binding.idlIdlPhoto.removeIcon(imageView);
                bean.content = "";
                bean.isPublic = false;
            });
            dialog.show();
        });
        binding.idlIdlPhoto.setOnIconClickListener(v -> {
            bean = (ImageDotLayout.IconBean) v.getTag();
            type = 2;
            inflate = LayoutInflater.from(SettingAboutUsActivity.this).inflate(R.layout.tag2_dialog, null);
            dialog = DialogUtils.showCenterDialog(SettingAboutUsActivity.this, inflate);
            goodsName = (EditText) inflate.findViewById(R.id.goodsNmae);
            spaceName = (TextView) inflate.findViewById(R.id.spaceName);
            tvDelete = (TextView) inflate.findViewById(R.id.tv_delete);
            TextView tvGoodsContent = (TextView) inflate.findViewById(R.id.tv_goodsContent);
            spaceName.setText(spaceNames);
            goodsName.setText(bean.content);
            goodsSwitch = (Switch) inflate.findViewById(R.id.goodsSwitch);
            goodsSwitch.setChecked(bean.isPublic);
            tv_Preservation = (Button) inflate.findViewById(R.id.tv_Preservation);
            goodsSwitch.setOnCheckedChangeListener(SettingAboutUsActivity.this);
            tv_Preservation.setOnClickListener(SettingAboutUsActivity.this);
            tvDelete.setOnClickListener(v1 -> {
                binding.idlIdlPhoto.removeIcon((TextView) v);
                if (dialog != null) dialog.dismiss();
                SpaceUtils.updateGoods(this, bean.content , bean.isPublic, bean.objectId , 1 );

            });
            tvGoodsContent.setOnClickListener(v12 -> {
                if (dialog != null) dialog.dismiss();
                ContentValues contentValues = new ContentValues();
                contentValues.put("objectId", bean.objectId);
                contentValues.put("belongGroupId", 0);
                contentValues.put("spaceId", 0);
                ActivityUtil.switchTo(SettingAboutUsActivity.this, MyselfEditSignatureActivity.class, contentValues);
            });
            dialog.show();
        });
        binding.tvTitleView.setOnTitleClickListener(mTitleType -> {
            if (TitleView.CLICK_BACK == mTitleType) {
                finish();
            }
        });
    }

    private void initIcon() {
        //监听图片是否加载完成
        binding.idlIdlPhoto.setOnLayoutReadyListener(() -> {
            initData();
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_Preservation:
                //保存
                if (dialog != null) dialog.setOnDismissListener(null);
                String goodsNames = goodsName.getText().toString().trim();
                if (TextUtils.isEmpty(goodsNames)) {
                    ToastUtils.showShortToast(this, "请输入物品名称");
                    return;
                }
                if (type == 1) {
                    //保存
                    ImageDotLayout.IconBean bean = (ImageDotLayout.IconBean) imageView.getTag();
                    bean.content = goodsNames;
                    bean.isPublic = isPublic;
                    AVObject avObject = new AVObject("Goods");
                    avObject.put("name", goodsNames);
                    avObject.put("isPublic", isPublic ? 1 : 0);
                    avObject.put("avatar", "");
                    avObject.put("positionId", objectId);
                    avObject.put("type", 2);
                    avObject.put("coordinate", coordinate);
                    avObject.saveInBackground(new SaveCallback() {
                        @Override
                        public void done(AVException e) {
                            if (e == null) {
                                bean.objectId = avObject.getObjectId();
                                Log.e("TAG", bean.toString());
                                EvenBusBean evenBusBean = new EvenBusBean(101);
                                EventBus.getDefault().post(evenBusBean);
                            } else {
                                ToastUtils.showShortToast(SettingAboutUsActivity.this, "位置上传失败上传失败..");
                            }
                        }
                    });

                } else {
                    //更新
                    bean.content = goodsNames;
                    bean.isPublic = isPublic;
                    SpaceUtils.updateGoods(this, goodsNames, isPublic, bean.objectId , 0);
                }
                if (dialog != null) dialog.dismiss();
                break;
            case R.id.tv_delete:
                break;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        isPublic = isChecked;
    }
}
